
package com.rt.schedulenew.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rt.schedulebase.dto.DictSmsContentDto;
import com.rt.schedulebase.entity.DictSmsContent;
import com.rt.schedulebase.mapper.DictSmsContentMapper;
import com.rt.schedulenew.api.IDictSmsContentService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.schedulenew.utils.base.CommonUtil;
import com.rt.schedulenew.utils.global.BusinessException;
import com.rt.schedulenew.utils.pkgenerator.PKGeneratorSingle;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class DictSmsContentServiceImpl extends ServiceImpl<DictSmsContentMapper, DictSmsContent> implements IDictSmsContentService {
        @Override
    public boolean insertSelective(DictSmsContent dictSmsContent) {
        // 检查是否重复
        QueryWrapper<DictSmsContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("EXAM_CLASS", dictSmsContent.getExamClass());
        Integer integer = baseMapper.selectCount(queryWrapper);
        if (Objects.nonNull(integer) && integer > 0) {
            throw new BusinessException("检查类别:" + dictSmsContent.getExamClass() + ",模板已存在!");
        }
        dictSmsContent.setSmsId(PKGeneratorSingle.getInstance().nextPKStr());
        return retBool(baseMapper.insert(dictSmsContent));
    }

    @Override
    public boolean deleteByPrimaryKey(String key) {
        return retBool(baseMapper.deleteById(key));
    }

    @Override
    public boolean updateByPrimaryKeySelective(DictSmsContent dictSmsContent) {
        QueryWrapper<DictSmsContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("EXAM_CLASS", dictSmsContent.getExamClass());
        queryWrapper.ne("SMS_ID", dictSmsContent.getSmsId());
        Integer integer = baseMapper.selectCount(queryWrapper);
        if (Objects.nonNull(integer) && integer > 0) {
            throw new BusinessException("检查类别:" + dictSmsContent.getExamClass() + ",模板已存在!");
        }
        return retBool(baseMapper.updateById(dictSmsContent));
    }

    @Override
    public DictSmsContent selectByPrimaryKey(String key) {
        return baseMapper.selectById(key);
    }
    @Override
    public List<DictSmsContent> getList(DictSmsContentDto dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("SMS_FLAG", dto.getSmsFlag());
        map.put("SMS_SIGN", dto.getSmsSign());
        map.put("SMS_TYPE", dto.getSmsType());
        map.put("PATIENT_SOURCE", dto.getPatientSource());
        QueryWrapper<DictSmsContent> qw = new QueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getExamClass())) {
            qw.in("EXAM_CLASS", CommonUtil.split2List(dto.getExamClass()));
        }
        qw.allEq(map, false);
        List<DictSmsContent> list = baseMapper.selectList(qw);
        return list;
    }

    @Override
    public List<DictSmsContent> getListAll() {
        return baseMapper.selectList(null);
    }
}
